package com.snail.readyvr.mvp.presenter;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.mvp.contract.RegisterActivityContract;
import com.snail.readyvr.mvp.model.EmailFindPswParam;
import com.snail.readyvr.mvp.model.EmailRegisterParam;
import com.snail.readyvr.mvp.model.GetEmailVerifyCodeParam;
import com.snail.readyvr.mvp.model.GetTokenResponse;
import com.snail.readyvr.mvp.model.LoginParam;
import com.snail.readyvr.mvp.model.PhoneRegisterParam;
import com.snail.readyvr.mvp.model.PhoneRegisterResponse;
import com.snail.readyvr.rest.HttpResultFunc;
import com.snail.readyvr.rest.LoginRestClient;
import com.snail.readyvr.rest.LoginUtils;
import com.snail.readyvr.rest.RestApiUtils;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/9.
 */

public class RegisterActivityPresenter implements RegisterActivityContract.Presenter {

    private RegisterActivityContract.View mView;
    private LoginRestClient mRestClient;

    @Inject
    public RegisterActivityPresenter(RegisterActivityContract.View view, LoginRestClient restClient) {
        mView = view;
        mRestClient = restClient;
    }

    @Override
    public void getPhoneVerifyCode(String phoneNum) {
        mRestClient.getPhoneVerifyCode(phoneNum,System.currentTimeMillis() + "")
                .map(new HttpResultFunc<GetTokenResponse>(RestApiUtils.GET_PHONE_VERIFYCODE_API))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GetTokenResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(GetTokenResponse response) {
                        if (mView != null)
                            mView.getPhoneVerifyCodeSuccess();
                    }
                });
    }

    @Override
    public void phoneRegister(PhoneRegisterParam param) {
        mRestClient.phoneRegister(System.currentTimeMillis() + "", param)
                .map(new HttpResultFunc<PhoneRegisterResponse>(RestApiUtils.PHONE_REGISTER_API))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PhoneRegisterResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(PhoneRegisterResponse phoneRegisterResponse) {
                        if(phoneRegisterResponse != null && phoneRegisterResponse.getUsername() != null) {
                            if (mView != null)
                                 mView.phoneRegisterSuccess();
                        }else {
                            if (mView != null)
                                 mView.error(new RuntimeException(MyApplication.getmContext().getString(R.string.code_default)));
                        }
                    }
                });
    }

    @Override
    public void phoneFindPsw(PhoneRegisterParam param) {
      mRestClient.phoneFindPsw(System.currentTimeMillis() + "",param)
              .map(new HttpResultFunc<PhoneRegisterResponse>(RestApiUtils.PHONE_FIND_PSW_API))
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Subscriber<PhoneRegisterResponse>() {
                  @Override
                  public void onCompleted() {

                  }

                  @Override
                  public void onError(Throwable e) {
                      if (mView != null)
                          mView.error(e);
                  }

                  @Override
                  public void onNext(PhoneRegisterResponse phoneRegisterResponse) {
                      if (mView != null)
                            mView.phoneFindPswSuccess();
                  }
              });
    }

    @Override
    public void autoLogin(int from, LoginParam param) {
        LoginUtils.getToken(mRestClient,from,param);
    }

    @Override
    public void emailRegister(EmailRegisterParam param) {
       mRestClient.emailRegister(param)
               .map(new HttpResultFunc<PhoneRegisterResponse>(RestApiUtils.EMAIL_REGISTER_API))
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Subscriber<PhoneRegisterResponse>() {
                   @Override
                   public void onCompleted() {

                   }

                   @Override
                   public void onError(Throwable e) {
                       if (mView != null)
                           mView.error(e);
                   }

                   @Override
                   public void onNext(PhoneRegisterResponse phoneRegisterResponse) {
                       if(phoneRegisterResponse != null && phoneRegisterResponse.getUsername() != null) {
                           if (mView != null)
                                mView.emailRegisterSuccess();
                       }else {
                           if (mView != null)
                             mView.error(new RuntimeException(MyApplication.getmContext().getString(R.string.code_default)));
                       }
                   }
               });
    }

    @Override
    public void getEmailVerifyCode(GetEmailVerifyCodeParam param) {

        mRestClient.getEmailVerifyCode(param)
                .map(new HttpResultFunc<PhoneRegisterResponse>(RestApiUtils.GET_EMAIL_VERIFYCODE_API))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PhoneRegisterResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                             mView.error(e);
                    }

                    @Override
                    public void onNext(PhoneRegisterResponse phoneRegisterResponse) {
                        if (mView != null)
                            mView.getEmailVerifyCodeSuccess();
                    }
                });
    }

    @Override
    public void emailFindPsw(EmailFindPswParam param) {
        mRestClient.emailFindPsw(param)
                .map(new HttpResultFunc<PhoneRegisterResponse>(RestApiUtils.EMAIL_FIND_PSW))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PhoneRegisterResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(PhoneRegisterResponse phoneRegisterResponse) {
                        if (mView != null)
                            mView.phoneFindPswSuccess();
                    }
                });

    }

    @Override
    public void start() {

    }
}
